package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.mapper.SpecGroupMapper;
import com.bwie.mapper.SpecParamMapper;
import com.bwie.pojo.SpecGroup;
import com.bwie.pojo.SpecParam;
import com.bwie.service.SpecService;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: day0423-pxx
 * @ClassName SpecServiceImpl
 * @description:
 * @author: 段阳
 * @create: 2024-04-28 16:56
 **/
@Service
@Transactional
public class SpecServiceImpl implements SpecService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;
    @Override
    public ResponseEntity<List<SpecGroup>> findSpecGroupList(Long id) {
        List<SpecGroup> list = specGroupMapper.selectList(new QueryWrapper<SpecGroup>().eq("cid", id));
        return ResponseEntity.ok(list);
    }

    @Override
    public List<SpecParam> findSpecParamList(Long gid, Long cid, Boolean generic, Boolean searching) {
        QueryWrapper<SpecParam> queryWrapper = new QueryWrapper<>();
        if (generic != null)
            queryWrapper.eq("generic", generic);
        if (searching != null)
            queryWrapper.eq("searching", searching);
        if (cid != null)
            queryWrapper.eq("cid", cid);
        if (gid != null)
            queryWrapper.eq("group_id", gid);



        List<SpecParam> specParams = specParamMapper.selectList(queryWrapper);
        return specParams;
    }

//    @Override
//    public ResponseEntity<List<SpecParam>> findSpecParamList(Long gid, Long cid) {
//        QueryWrapper<SpecParam> specParamQueryWrapper = new QueryWrapper<>();
//        if (gid!=null){
//            specParamQueryWrapper.lambda().eq(SpecParam::getGroupId, gid);
//        }
//        if (cid!=null){
//            specParamQueryWrapper.lambda().eq(SpecParam::getCid, cid);
//        }
//        List<SpecParam> paramList = specParamMapper.selectList(specParamQueryWrapper);
//        return ResponseEntity.ok(paramList);
//    }

    @Override
    public R addSpecParam(SpecParam specParam) {
        specParamMapper.insert(specParam);
        return R.success("success");
    }


    @Override
    public R addSpecGroup(SpecGroup specGroup) {
        specGroupMapper.insert(specGroup);
        return R.success("success");
    }

    @Override
    public R upSpecGroup(SpecGroup specGroup) {
        specGroupMapper.updateById(specGroup);
        return R.success("success");
    }

    @Override
    public R upSpecParam(SpecParam specParam) {
        specParamMapper.updateById(specParam);
        return R.success("success");
    }

    @Override
    public R delSpecParam(Long id) {
        specParamMapper.deleteById(id);
        return R.success("success");
    }

    @Override
    public R delSpecGroup(Long id) {
        specParamMapper.delete(new QueryWrapper<SpecParam>().lambda().eq(SpecParam::getGroupId,id));
        specGroupMapper.deleteById(id);
        return R.success("success");
    }
}